﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;


namespace Exe1
{
    public class MyContainer
    {
        public virtual bool add(itm v)
        {
            return true;
        }
        public virtual itm get(int idx)
        {
            itm v;
            v.title = "";
            v.authors = "";
            v.zhaiyao = "";
            return v;
        }
        public virtual bool set(int idx, itm v)
        {
            bool rt = true;

            return rt;
        }
        public virtual int getCount()
        {
            int rt = 0;
            return rt;
        }
        public virtual bool remove(int idx)
        {
            return true;
        }
        public virtual ArrayList query(string queryS)
        {
            ArrayList rt = new ArrayList();

            return rt;
        }
        public bool CompString(string targetS, string queryS)
        {
            bool rt = false;

            // judge wether targetS contains queryS
            int pos = 0;
            int pLim = targetS.Length - queryS.Length;
            int i;
            bool bMatch = false;
            while (pos <= pLim)
            {
                if (targetS[pos] == queryS[0])
                {
                    bMatch = true;
                    for (i = 1; i < queryS.Length; i++)
                    {
                        if (targetS[pos + i] != queryS[i])
                        {
                            bMatch = false;
                            break;
                        }
                    }
                    if (bMatch)
                    {
                        rt = true;
                        break;
                    }

                }
                pos++;
            }

            return rt;
        }
    }

    public class MyArrayContainer: MyContainer
    {
        private itm[] _data;
        private int _count;

        public MyArrayContainer()
        {
            _data = new itm[3];
            _count = 0;
        }

        public override bool add(itm v)
        {
            if (_count < 3)
            {
                _data[_count] = v;
                _count++;
            }
            else
                return false;            

            return base.add(v);
        }
        public override itm get(int idx)
        {
            itm rt = _data[idx];

            return rt;
        }
        public override bool set(int idx, itm v)
        {
            bool rt = true;
            if (idx < _count)
            {
                _data[idx] = v;
            }
            else
                return false;

            return rt;
        }
        public override int getCount()
        {
            return _count;
        }
        public override bool remove(int idx)
        {
            // omit
            return base.remove(idx);
        }
        public override ArrayList query(string queryS)
        {
            ArrayList rt = new ArrayList();
            for (int i = 0; i < _count; i++ )
            {
                if (CompString(_data[i].title, queryS) || CompString(_data[i].authors, queryS)||CompString(_data[i].zhaiyao, queryS))
                {
                    rt.Add(_data[i]);
                }
            }

            return rt;
        }
    }

    public class MyALContainer: MyContainer
    {
        private ArrayList _data;

        public MyALContainer()
        {
            _data = new ArrayList();
        }
        public override bool add(itm v)
        {
            bool rt = true;

            _data.Add(v);

            return rt;
        }
        public override itm get(int idx)
        {
            itm v;
            v = (itm)_data[idx];
            return v;
        }
        public override bool set(int idx, itm v)
        {
            bool rt = true;

            _data[idx] = v;

            return rt;
        }
        public override int getCount()
        {
            return _data.Count;
        }
        public override bool remove(int idx)
        {
            return base.remove(idx);
        }
        public override ArrayList query(string queryS)
        {
            ArrayList rt = new ArrayList();
            itm tmp;

            for (int i = 0; i < _data.Count; i++ )
            {
                tmp = (itm)_data[i];
                if (CompString(tmp.title, queryS)||CompString(tmp.authors, queryS)||CompString(tmp.zhaiyao, queryS))
                {
                    rt.Add(tmp);
                }
            }

            return rt;
        }
    }
}
